package org.example;

import javax.swing.filechooser.FileFilter;
import java.io.File;

public class FileExtNameFilter extends FileFilter {

    private String extName;

    public FileExtNameFilter(String extName){
        this.extName = extName;
    }

    @Override
    public boolean accept(File f) {

        if(f.isDirectory()){
            return true;
        }

        String fileName = f.getName();
        int index = fileName.lastIndexOf(".");
        if(index>0 && index < fileName.length() -1){
            String extension = fileName.substring(index+1).toLowerCase();
            if (extension.equals(extName)){
                return true;
            }
        }
        return false;
    }

    @Override
    public String getDescription() {
        if(extName.equals("xlsx")){
            return "Excel(*.xlsx)";
        }
        if(extName.equals("xls")){
            return "Excel(*.xls)";
        }
        return "";
    }
}
